<?php
class Promotion_model extends CI_Model{
	function __construct(){
		parent::__construct();
	}

	public function get_all(){
		$this->db->select('item_barcode, promotion_id, promotion_unit, promotion_discount, promotion_active, promotion_created_date, promotion_modified_date');
		$this->db->from('promotion');
		$this->db->join('item', 'promotion.item_id = item.item_id');
		$query = $this->db->get();
		return $query->result();
		
	}

	public function get_updated($day){
		$this->db->select('item_barcode, promotion_id, promotion_unit, promotion_discount, promotion_active, promotion_created_date, promotion_modified_date');
		$this->db->from('promotion');
		$this->db->join('item', 'promotion.item_id = item.item_id');
		$this->db->where('promotion_modified_date >= DATE_SUB(NOW(), INTERVAL '.$day.' DAY)');
		$query = $this->db->get();
		return $query->result();
	}

	public function add($item_id, $promotion_unit, $promotion_discount, $active){
		$data = array(
				'item_id' => $item_id,
				'promotion_unit' => $promotion_unit,
				'promotion_discount' => $promotion_discount,
				'promotion_active' => $active
		);

		if ($this->db->insert('promotion', $data))
			return true;
		else
			return false;
	}

	public function update($item_id, $promotion_unit, $promotion_discount, $active){
		$data = array(
				'promotion_unit' => $promotion_unit,
				'promotion_discount' => $promotion_discount,
				'promotion_active' => $active
		);
		$this->db->set('promotion_modified_date', 'now()', false);
		$this->db->where('item_id', $item_id);
		if ($this->db->update('promotion', $data))
			return true;
		else
			return false;
	}
	public function update_discount($item_id, $promotion_discount){
		$data = array(
				'promotion_discount' => $promotion_discount
		);
		$this->db->set('promotion_modified_date', 'now()', false);
		$this->db->where('item_id', $item_id);
		if ($this->db->update('promotion', $data))
			return true;
		else
			return false;
	}
	
}